import addressData from './address-data.min'
/* 返回所有国家，正则1-4位数字*/
function getNation() {
  const data = addressData.filter(item => /^[1-9]\d{0,3}$/.test(item.code))
  data.forEach(item => (item.children = []))
  return data
}
/**
 * 返回所有省份
 */
function getProvince() {
  const data = addressData.filter(item => /[0-9]{2}0000$/.test(item.code))
  data.forEach(item => (item.children = []))
  return data
}

/**
 * 返回所有城市
 * @param {*} parent 父级
 * @param {*} auto 是否自动级别，不强制三级联动
 */
function getCity(parent, auto) {
  const p = parent.code.substring(0, 2)
  const reg = `^${p}([0-9][1-9]|[1-9][0])00$`
  const filter_ = new RegExp(reg)
  const data = addressData.filter(item => filter_.test(item.code))
  data.forEach(item => (item.children = []))
  if (data.length > 0) return data
  return auto ? getCounty(parent) : [parent]
}

/**
 * 返回所有县/区
 * @param {*} parent 父级id
 */
function getCounty(parent) {
  const p_f = parent.code.substring(0, 2)
  const p_n = parent.code.substring(2, 4)
  const p = p_n === '00' ? `${p_f}01` : `${p_f}${p_n}`
  const reg = `^${p}([0-9][1-9]|[1-9][0])$`
  const filter_ = new RegExp(reg)
  const data = addressData.filter(item => filter_.test(item.code))
  return data
}

/**
 * 解析地址
 * @param {*} data
 */
function analysisAddress(data) {
  if (!data) return ''
  let address = ''
  try {
    address = JSON.parse(data) || []
  } catch (error) {
    console.log('地址信息不合规范:' + error)
  }
  // let address = JSON.parse(data) || [];
  if (address instanceof Array && address.length > 0) {
    const [
      province_act = { name: '' },
      city_act = { name: '' },
      county_act = { name: '' }
    ] = address

    return province_act.code !== city_act.code
      ? province_act.name + ' ' + city_act.name + ' ' + county_act.name
      : province_act.name + ' ' + county_act.name
  }
  return ''
}

export { getProvince, getCity, getCounty, analysisAddress, getNation }
